//
//  VNCPrefsView.m
//  vnsea
//
//  Created by Glenn Kreisel on 11/3/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "VnseaApp.h"
#import "VNCPrefsView.h"
#import <UIKit/UIPreferencesTextTableCell.h>
#import <UIKit/UIPreferencesControlTableCell.h>
#import <UIKit/UINavigationItem.h>
#import "VNCPreferences.h"

//! @brief Navigation bar button index for the Back button.
#define kServerListButton 1

//! Converts a boolean value to the floating point on or off value for a UISwitchControl.
#define BOOL_TO_SWITCH_VALUE(b) ((b) ? 1.0f : 0.0f)

//! Takes a UISwitchControl and returns a boolean from its value.
//!
//! We use a greater than comparison here because floats are not always exact.
#define SWITCH_VALUE_TO_BOOL(s) ([(s) value] > 0.1)

@implementation VNCPrefsView

- (id)initWithFrame:(CGRect)frame
{
	if ([super initWithFrame:frame])
	{
		frame.size.height-=20;
		frame.origin.y+=20;
		CGRect subviewFrame;
		
		// Create nav bar
		subviewFrame = CGRectMake(0.0f, 20.0f, frame.size.width, 48);
		_navBar = [[UINavigationBar alloc] initWithFrame:subviewFrame];
		[_navBar showButtonsWithLeftTitle:NSLocalizedString(@"Back", nil) rightTitle:nil leftBack: YES];
		[_navBar setBarStyle: 3];
		[_navBar setDelegate: self];
		[self addSubview: _navBar];
		
		UINavigationItem * item = [[UINavigationItem alloc] initWithTitle:NSLocalizedString(@"Preferences", nil)];
		[_navBar pushNavigationItem:item];
		
		// Create preferences table
		subviewFrame = CGRectMake(0, 20+48, frame.size.width, frame.size.height - 48);
		_table = [[UIPreferencesTable alloc] initWithFrame:subviewFrame];
		[_table setDataSource:self];
		[_table setDelegate:self];
		[self addSubview:_table];
		
		CGPoint controlOrigin;
/*		
		UIPreferencesControlTableCell * zoomFastCell = [[UIPreferencesControlTableCell alloc] init];
		[zoomFastCell setTitle:NSLocalizedString(@"Quick Zoom", nil)];
		controlOrigin = CGPointMake(200, 9);
		_zoomFastSwitch = [[UISwitchControl alloc] init];
		[_zoomFastSwitch setOrigin:controlOrigin];
		[zoomFastCell setControl:_zoomFastSwitch];

		UIPreferencesControlTableCell * scrollFastCell = [[UIPreferencesControlTableCell alloc] init];
		[scrollFastCell setTitle:NSLocalizedString(@"Quick Scroll", nil)];
		controlOrigin = CGPointMake(200, 9);
		_scrollFastSwitch = [[UISwitchControl alloc] init];
		[_scrollFastSwitch setOrigin:controlOrigin];
		[scrollFastCell setControl:_scrollFastSwitch];
*/
		UIPreferencesControlTableCell * scrollInvertCell = [[UIPreferencesControlTableCell alloc] init];
		[scrollInvertCell setTitle:NSLocalizedString(@"Invert Scroll", nil)];
		controlOrigin = CGPointMake(200, 9);
		_scrollInvertSwitch = [[UISwitchControl alloc] init];
		[_scrollInvertSwitch setOrigin:controlOrigin];
		[scrollInvertCell setControl:_scrollInvertSwitch];

		UIPreferencesControlTableCell * useTextIconsCell = [[UIPreferencesControlTableCell alloc] init];
		[useTextIconsCell setTitle:NSLocalizedString(@"Text Icons", nil)];
		controlOrigin = CGPointMake(200, 9);
		_useTextIconsSwitch = [[UISwitchControl alloc] init];
		[_useTextIconsSwitch setOrigin:controlOrigin];
		[useTextIconsCell setControl:_useTextIconsSwitch];

		UIPreferencesControlTableCell * allowRotationCell = [[UIPreferencesControlTableCell alloc] init];
		[allowRotationCell setTitle:NSLocalizedString(@"Allow Rotation", nil)];
		controlOrigin = CGPointMake(200, 9);
		_allowRotationSwitch = [[UISwitchControl alloc] init];
		[_allowRotationSwitch setOrigin:controlOrigin];
		[allowRotationCell setControl:_allowRotationSwitch];

		UIPreferencesControlTableCell * enableInertiaCell = [[UIPreferencesControlTableCell alloc] init];
		[enableInertiaCell setTitle:NSLocalizedString(@"Enable Inertia", nil)];
		controlOrigin = CGPointMake(200, 9);
		_enableInertiaSwitch = [[UISwitchControl alloc] init];
		[_enableInertiaSwitch setOrigin:controlOrigin];
		[enableInertiaCell setControl:_enableInertiaSwitch];

		UIPreferencesControlTableCell * enableScrollCell = [[UIPreferencesControlTableCell alloc] init];
		[enableScrollCell setTitle:NSLocalizedString(@"Thumb Scrolling", nil)];
		controlOrigin = CGPointMake(200, 9);
		_enableScrollSwitch = [[UISwitchControl alloc] init];
		[_enableScrollSwitch setOrigin:controlOrigin];
		[enableScrollCell setControl:_enableScrollSwitch];

		_maxWidthCell = [[UIPreferencesTextTableCell alloc] init];
		[[_maxWidthCell textField] setReturnKeyType: 4];
		[[_maxWidthCell textField] setPreferredKeyboardType: 2];
		[_maxWidthCell setTitle:NSLocalizedString(@"MaxWidth", nil)];
		[_maxWidthCell setPlaceHolderValue:kDefaultWidth];

		_maxHeightCell = [[UIPreferencesTextTableCell alloc] init];
		[[_maxHeightCell textField] setReturnKeyType: 4];
		[[_maxHeightCell textField] setPreferredKeyboardType: 2];
		[_maxHeightCell setTitle:NSLocalizedString(@"MaxHeight", nil)];
		[_maxHeightCell setPlaceHolderValue:kDefaultHeight];

		UIPreferencesControlTableCell * macCPUCell = [[UIPreferencesControlTableCell alloc] init];
		[macCPUCell setTitle:NSLocalizedString(@"MacCPUFix", nil)];
		controlOrigin = CGPointMake(200, 9);
		_macCPUSwitch = [[UISwitchControl alloc] init];
		[_macCPUSwitch setOrigin:controlOrigin];
		[macCPUCell setControl:_macCPUSwitch];

		_licenseKeyCell = [[UIPreferencesTextTableCell alloc] init];
		[_licenseKeyCell setTitle:NSLocalizedString(@"License Key", nil)];
		[_licenseKeyCell setPlaceHolderValue:NSLocalizedString(@"License Key Place", nil)];
//		[[_licenseKeyCell textField] setReturnKeyType: 4];

		// Disconnect on suspend cell.
		UIPreferencesControlTableCell * disconnectCell = [[UIPreferencesControlTableCell alloc] init];
		[disconnectCell setTitle:NSLocalizedString(@"Exit App on Suspend", nil)];
		
//		controlOrigin = CGPointMake(200, 9);
		_disconnectSwitch = [[UISwitchControl alloc] init];
		[_disconnectSwitch setOrigin:controlOrigin];
		[disconnectCell setControl:_disconnectSwitch];
		
		// Create array of cells.
		_cells = [[NSArray arrayWithObjects: allowRotationCell, enableInertiaCell, enableScrollCell, scrollInvertCell, disconnectCell, macCPUCell, _maxWidthCell, _maxHeightCell, nil] retain];
	}
	
	return self;
}

- (void)dealloc
{
	[_table release];
	[_navBar release];
	[_cells release];
	
	[super dealloc];
}

- (void)setDelegate:(id)newDelegate
{
	_delegate = newDelegate;
}

- (id)delegate
{
	return _delegate;
}

- (void)setKeyboardVisible:(BOOL)visible
{
	[_table setKeyboardVisible:visible animated:YES];
}

- (void)updateViewFromPreferences
{
	// Update cell values from the prefs info
	[_enableScrollSwitch setValue:BOOL_TO_SWITCH_VALUE([[VNCPreferences sharedPreferences] enableScroll])];
	[_enableInertiaSwitch setValue:BOOL_TO_SWITCH_VALUE([[VNCPreferences sharedPreferences] enableInertia])];
	[_allowRotationSwitch setValue:BOOL_TO_SWITCH_VALUE([[VNCPreferences sharedPreferences] allowRotation])];
	[_useTextIconsSwitch setValue:BOOL_TO_SWITCH_VALUE([[VNCPreferences sharedPreferences] useTextIcons])];
	[_scrollInvertSwitch setValue:BOOL_TO_SWITCH_VALUE([[VNCPreferences sharedPreferences] scrollInvert])];
//	[_zoomFastSwitch setValue:BOOL_TO_SWITCH_VALUE([[VNCPreferences sharedPreferences] shouldZoomFast])];
//	[_scrollFastSwitch setValue:BOOL_TO_SWITCH_VALUE([[VNCPreferences sharedPreferences] shouldScrollFast])];
	[_disconnectSwitch setValue:BOOL_TO_SWITCH_VALUE([[VNCPreferences sharedPreferences] disconnectOnSuspend])];
	[_maxWidthCell setValue: ([[VNCPreferences sharedPreferences] maxWidth] != [kDefaultWidth intValue] ? [NSString stringWithFormat:@"%d", [[VNCPreferences sharedPreferences] maxWidth]] : @"")];
	[_maxHeightCell setValue: ([[VNCPreferences sharedPreferences] maxHeight] != [kDefaultHeight intValue] ? [NSString stringWithFormat:@"%d", [[VNCPreferences sharedPreferences] maxHeight]] : @"")];
	[_macCPUSwitch setValue:BOOL_TO_SWITCH_VALUE([[VNCPreferences sharedPreferences] macCPUFix])];
	[_licenseKeyCell setValue:[[VNCPreferences sharedPreferences] licenseKey]];
	
	[_table reloadData];
}

- (void)navigationBar:(id)navBar buttonClicked:(int)buttonIndex
{
	switch (buttonIndex)
	{
		// Save Prefs and go back
		case kServerListButton:
		{
			bool resizewarning = false;
			int width = [([[_maxWidthCell value] isEqualToString: @""] ? kDefaultWidth : [_maxWidthCell value]) intValue];
			int height = [([[_maxHeightCell value] isEqualToString: @""] ? kDefaultHeight : [_maxHeightCell value]) intValue];
			if (width > 2500)
			{
				resizewarning = true;
				width = 2500;
			}
			if (height > 2500)
			{
				resizewarning = true;
				height = 2500;
			}
			if (width * height > (1280*1200))
			{
				resizewarning = true;
				height = 1280*1200 / width;
			}

			if ((width > 1280 && width != [[VNCPreferences sharedPreferences] maxWidth]) || (height != [[VNCPreferences sharedPreferences] maxHeight] && height > 1200) || resizewarning)
			{
				UIAlertSheet *errorPopup = [[UIAlertSheet alloc ] initWithFrame:CGRectMake(0,0,250,350) ];	
				[errorPopup setTitle:NSLocalizedString(@"Warning", nil)];
				[errorPopup setDelegate:self];
				[errorPopup setContext:self];
				[errorPopup setDimsBackground:YES];
				[errorPopup addButtonWithTitle:NSLocalizedString(@"OK", nil)];

				[errorPopup _slideSheetOut:YES];
				[errorPopup layoutAnimated:NO];
				[errorPopup setBodyText:[NSString stringWithFormat: NSLocalizedString(@"ScreenViewSizeWarning", nil), (resizewarning?[NSString stringWithFormat: NSLocalizedString(@"ScreenViewResized",nil), width, height]:@"")]];
				[errorPopup popupAlertAnimated:YES atOffset:0.0];
				[errorPopup setAlpha:1.0f];
			}

			[[VNCPreferences sharedPreferences] setEnableScroll:SWITCH_VALUE_TO_BOOL(_enableScrollSwitch)];
			[[VNCPreferences sharedPreferences] setEnableInertia:SWITCH_VALUE_TO_BOOL(_enableInertiaSwitch)];
			[[VNCPreferences sharedPreferences] setAllowRotation:SWITCH_VALUE_TO_BOOL(_allowRotationSwitch)];
			[[VNCPreferences sharedPreferences] setUseTextIcons:SWITCH_VALUE_TO_BOOL(_useTextIconsSwitch)];
			[[VNCPreferences sharedPreferences] setScrollInvert:SWITCH_VALUE_TO_BOOL(_scrollInvertSwitch)];
//			[[VNCPreferences sharedPreferences] setShouldZoomFast:SWITCH_VALUE_TO_BOOL(_zoomFastSwitch)];
//			[[VNCPreferences sharedPreferences] setShouldScrollFast:SWITCH_VALUE_TO_BOOL(_scrollFastSwitch)];
			[[VNCPreferences sharedPreferences] setDisconnectOnSuspend:SWITCH_VALUE_TO_BOOL(_disconnectSwitch)];
			[[VNCPreferences sharedPreferences] setMaxWidth: width];
			[[VNCPreferences sharedPreferences] setMaxHeight: height];
			[[VNCPreferences sharedPreferences] setMacCPUFix:SWITCH_VALUE_TO_BOOL(_macCPUSwitch)];
			[[VNCPreferences sharedPreferences] setLicenseKey: [_licenseKeyCell value]];
		
			break;
		}
	}
	
	if (_delegate && [_delegate respondsToSelector:@selector(finishedEditingPreferences)])
	{
		[_delegate finishedEditingPreferences];
	}
}

- (void) alertSheet:(id)sheet buttonClicked:(int)button
{
	[sheet dismissAnimated:YES];
//	[sheet release];
}

- (int)numberOfGroupsInPreferencesTable:(id)fp8
{
	return 1;
}

- (id)preferencesTable:(id)prefsTable cellForRow:(int)rowIndex inGroup:(int)groupIndex
{
	if (groupIndex == 0)
	{
		return [_cells objectAtIndex:rowIndex];
	}
}

- (int)preferencesTable:(id)prefsTable numberOfRowsInGroup:(int)groupIndex
{
	if (groupIndex == 0)
	{
		return [_cells count];
	}
	else
	{
		return 1;
	}
}

- (BOOL)table:(id)prefsTable showDisclosureForRow:(int)rowIndex
{
	return NO;
}

@end
